class Blog < ActiveRecord::Base
  
  #consts
  #首页显式blog数量，现在设置为5
  BLOGS_ON_INDEX_PAGE=5
  #列表显式blog数量，现在设置为5
  BLOGS_ON_LIST_PAGE=10
  
  #associations
  belongs_to :category, :counter_cache=>true
  has_many :comments, :dependent=>:destroy,:as=>:resource
  
  #methods
  def self.paginate_index(page)
    paginate(:per_page=>BLOGS_ON_INDEX_PAGE, 
      :page=>page, 
      :order=>'created_at DESC')
  end
  
  def self.paginate_category(category_id,page)
    conditions= category_id== nil ? "category_id is null" : 
    "category_id=#{category_id}"
    paginate(:per_page=>BLOGS_ON_LIST_PAGE, 
      :page=>page, 
      :conditions=>conditions,
      :order=>'created_at DESC')
  end
    
  def self.paginate_list(page)
    @blogs=Blog.paginate(:per_page=>BLOGS_ON_LIST_PAGE, 
      :page=>page,
      :order=>'created_at DESC')
  end
  
end
